package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.params.PageQueryParam;
import com.itheima.common.vo.PageResult;
import com.itheima.common.vo.Result;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = "1.0",interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Resource
    private CheckGroupMapper checkGroupMapper;

    @Override
    public Result findPage(PageQueryParam pageQueryParam) {
        PageHelper.startPage(pageQueryParam.getCurrentPage(),pageQueryParam.getPageSize());

        Page<CheckGroup> checkGroupPage = checkGroupMapper.findByCondition(pageQueryParam.getQueryString());

        PageResult pageResult = new PageResult();
        pageResult.setList(checkGroupPage.getResult());
        pageResult.setTotal(checkGroupPage.getTotal());
        return Result.success(pageResult);
    }

    @Override
    @Transactional
    public Result save(CheckGroup checkGroup) {
        //1. 新增 检查组表
        //2. 新增关联表
        this.checkGroupMapper.save(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        saveGroupAndItem(checkGroup, checkGroupId);
        return Result.success(null);
    }

    @Autowired
    private CheckItemMapper checkItemMapper;
    @Override
    public Result findAll(Integer checkGroupId) {
        // this.tableData = res.data.data.checkItemList;
        //                            this.checkItemIds = res.data.data.checkItemIds;
        //                            this.formData = res.data.data.checkGroup;
        List<CheckItem> checkItemList = checkItemMapper.findAll();
        List<Integer> checkItemIds = checkGroupMapper.findItemIdsByGroupId(checkGroupId);
        CheckGroup checkGroup = checkGroupMapper.findById(checkGroupId);
        Map<String,Object> map = new HashMap<>();
        map.put("checkItemList",checkItemList);
        map.put("checkItemIds",checkItemIds);
        map.put("checkGroup",checkGroup);
        return Result.success(map);
    }

    @Override
    @Transactional
    public Result update(CheckGroup checkGroup) {
        //1. 编辑检查组表
        //2. 先删除检查组和检查项的关联表 然后再新增
        this.checkGroupMapper.update(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        this.checkGroupMapper.deleteGroupAndItemByGroupId(checkGroupId);
        saveGroupAndItem(checkGroup, checkGroupId);
        return Result.success(null);
    }

    @Override
    public Result findAllCheckGroup() {
        List<CheckGroup> checkGroupList = this.checkGroupMapper.findAll();
        return Result.success(checkGroupList);
    }

    private void saveGroupAndItem(CheckGroup checkGroup, Integer checkGroupId) {
        List<Integer> checkItemIds = checkGroup.getCheckItemIds();
        for (Integer checkItemId : checkItemIds) {
            this.checkGroupMapper.saveCheckGroupAndItem(checkGroupId, checkItemId);
        }
    }
}
